home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / Utilities / GrabDrivers / GrabVideoDrivers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-10  |  6.4 KB  |  189 lines  |  [TEXT/KAHL]

  1. /*
  2. GrabVideoDrivers.c
  3. Gets each active video driver and saves it in a file of the same name, using the
  4. driver's version number as a resource id. These files can later be examined in
  5. ResEdit, using the public domain CODE viewer. You'll want to consult Apple's
  6. Designing Cards and Drivers, 3rd Ed., Addison Wesley.
  7.  
  8. The ResEdit CODE viewer is a public domain file, for use with ResEdit,
  9. distributed by:
  10. Ira L. Ruben
  11. Apple Computer, Inc.
  12. 20525 Mariani Ave., MS: 37-A
  13. Cupertino, Ca. 95014
  14. Ira@Apple.Com
  15.  
  16. Copyright restrictions prevent us from distributing video drivers themselves,
  17. but this program makes it easy for you to get an accessible copy of all the
  18. video drivers used in your own computer.
  19.  
  20. The primary users of this program will probably be those who wish to enhance
  21. SetEntriesQuickly.c. I hope that people will share the fruits of their labors by
  22. sending me the enhancements for distribution in the VideoToolbox disk.
  23.  
  24. This program is also useful for copying the Mac IIsi video driver to the Mac
  25. IIci. Since that's a ROM-based driver I don't know any way to automatically
  26. determine its size, and instead this program uses a generous guess. Consequently
  27. the resulting resource is larger than it needs to be. When you install this
  28. resource in the System file of your Mac IIci the excess space is wasted (both on
  29. disk and in memory), but the excess is only about 1 kB, so it doesn't seem worth
  30. worrying about.
  31.  
  32. HISTORY:
  33. 2/89 dgp Wrote it as "video hacker II.c" using THINK "Light Speed" C 3.
  34. 12/30/92 dgp updated it to THINK C 5 and renamed it GrabVideoDrivers.c
  35. 7/29/94 dgp Eliminated use of "#s" printf format, since it's not supported by
  36.             Metrowerks CodeWarrior C.
  37. 9/5/94 dgp removed assumption in printf's that int==short.
  38. */
  39. #include "VideoToolbox.h"
  40. #include <Errors.h>
  41. #include <Files.h>
  42. #include <Resources.h>
  43. #define dRAMBased        0x0040
  44. Handle GetDriverFromSlotManager(GDHandle device);
  45.  
  46. void main(void);
  47. void CopyDeviceDriver(GDHandle device);
  48. void AddResourceToFile(unsigned char *filename,unsigned char *name,ResType type
  49.     ,int id,Handle handle);
  50. typedef struct {
  51.     short flags;
  52.     short blanks[3];
  53.     short open;
  54.     short prime;
  55.     short control;
  56.     short status;
  57.     short close;
  58.     Str255 name;
  59. } VideoDriver;
  60. VideoDriver *GDDriverAddress(GDHandle device);
  61.  
  62. void main()
  63. {
  64.     GDHandle device;
  65.     int i;
  66.     
  67.     MaximizeConsoleHeight();
  68.     printf("Welcome to GrabVideoDriver.\n");
  69.     printf("This program cycles through all your video devices and copies\n"
  70.     "each driver into a resource file, suitable for subsequent examination\n"
  71.     "with ResEdit (with the CODE viewer). This is primarily of interest for people\n"
  72.     "who want to enhance SetEntriesQuickly.c to support more video devices.\n");
  73.     for(i=0;i<10;i++){
  74.         device=GetScreenDevice(i);
  75.         if(device==NULL)break;
  76.         CopyDeviceDriver(device);
  77.     }
  78.     printf("\nDone.\n");
  79. }
  80.  
  81. void CopyDeviceDriver(GDHandle device)
  82. {
  83.     char *name;
  84.     char filename[32];
  85.     long driverSize;
  86.     AuxDCE **myAuxDCEHandle;
  87.     Handle handle;
  88.     VideoDriver *driver;
  89.     unsigned char *bytePtr;
  90.     int version;
  91.     
  92.     if(device==NULL)return;
  93.     printf("\n");
  94.     name=GDNameStr(device);
  95.     version=GDVersion(device);
  96.     printf("%s version %d.\n",name,version);
  97.     strncpy(filename,name,31);
  98.     // filename must be truncated to 31 characters */
  99.     filename[32]=0;
  100.     // Replace leading "." by "-" since the Mac filing system 
  101.     // is confused by filenames beginning with ".".
  102.     if(filename[0]=='.')filename[0]='-';
  103.  
  104.     driver=GDDriverAddress(device);
  105.     printf("Offsets: open 0x%x, prime 0x%x, control 0x%x, status 0x%x, close 0x%x\n"
  106.         ,driver->open,driver->prime,driver->control,driver->status,driver->close);
  107.     printf("name is at 0x%lx, ",driver->name-(unsigned char *) driver);
  108.     bytePtr=driver->name;
  109.     bytePtr += 1+bytePtr[0];    /* go to end of Pascal string */
  110.     bytePtr = (unsigned char *)((long)(bytePtr+1) & ~1);    // round up to word boundary
  111.     printf("version is at 0x%lx\n",bytePtr-(unsigned char *) driver);
  112.  
  113.     myAuxDCEHandle = (AuxDCE **) GetDCtlEntry((*device)->gdRefNum);
  114.     if((**myAuxDCEHandle).dCtlFlags & dRAMBased){
  115.         printf("RAM-based driver.\n");
  116.         handle=(Handle)(**myAuxDCEHandle).dCtlDriver;
  117.         HandToHand(&handle);
  118.         printf("%ld bytes\n",GetHandleSize(handle));
  119.     }
  120.     else{
  121.         printf("ROM-based driver.\n");
  122.         handle=GetDriverFromSlotManager(device);
  123.         if(handle!=NULL){
  124.             printf("Got driver with help from Slot Manager.\n");
  125.             printf("%ld bytes\n",GetHandleSize(handle));
  126.         }else{
  127.             // We have a Ptr to the driver, but we don't know how big it is.
  128.             driverSize=(unsigned long) GetPtrSize((Ptr)(**myAuxDCEHandle).dCtlDriver);
  129.             if(driverSize==0 || MemError()){
  130.                 driverSize=driver->open;
  131.                 if(driverSize<driver->prime)driverSize=driver->prime;
  132.                 if(driverSize<driver->control)driverSize=driver->control;
  133.                 if(driverSize<driver->status)driverSize=driver->status;
  134.                 if(driverSize<driver->close)driverSize=driver->close;
  135.                 driverSize*=2;
  136.                 printf("Size unknown, guessing (generously) at %ld, twice the highest offset.\n",driverSize);
  137.             }else printf("%ld bytes\n",driverSize);
  138.             PtrToHand((**myAuxDCEHandle).dCtlDriver,&handle,driverSize);
  139.         }
  140.     }
  141.     if(handle!=NULL){
  142.         AddResourceToFile(c2pstr(filename),c2pstr(name),'DRVR',version,handle);
  143.         printf("Driver copied to ā€œ%sā€ file, using the version number %d as the resource id.\n"
  144.             ,p2cstr((unsigned char *)filename),version);
  145.         DisposHandle(handle);
  146.     }else printf("Couldn't copy driver.\n");
  147. }
  148.  
  149. /*
  150. This gets a copy of the driver from the slot manager. Returns NULL unless we can
  151. find exactly the same driver as is specified by the supplied GDHandle. We check
  152. every byte. This is not a useless operation, because although we already have
  153. the address of the driver, we don't necessarily already have its size, and the
  154. slot manager will supply us with a handle, from which we can obtain the size.
  155. */
  156. Handle GetDriverFromSlotManager(GDHandle device)
  157. {
  158.     SpBlock mySpBlock;
  159.     SEBlock mySEBlock;
  160.     unsigned char *desiredName,name[256];
  161.     int error;
  162.     Ptr *handle;
  163.     
  164.     desiredName=GDName(device);
  165.     mySpBlock.spsExecPBlk = (Ptr) &mySEBlock;
  166.     mySpBlock.spSlot = 0;
  167.     mySpBlock.spID = 0;
  168.     mySpBlock.spExtDev = 0;
  169.     while(1){
  170.         error = SNextSRsrc(&mySpBlock);
  171.         if(error==smNoMoresRsrcs) break;
  172.         if(error){
  173.             printf("SNextSRsrc error %d\n",error);
  174.             break;
  175.         }
  176.         mySpBlock.spResult = (unsigned long) &name;
  177.         error = SReadDrvrName(&mySpBlock);
  178.         if(!EqualString(desiredName,name,1,1))continue;
  179.         error = SGetDriver(&mySpBlock);
  180.         if(!error)continue;
  181.         handle = (Handle) mySpBlock.spResult;
  182.         if(memcmp(*handle,GDDriverAddress(device),GetHandleSize(handle))!=0){
  183.             DisposHandle(handle);
  184.             break;
  185.         }
  186.         return handle;
  187.     }
  188.     return NULL;
  189. }